// component/underway/underway.ts
import { loadingOrder, todayOrder, searchMaintainer, orderTransfer } from '../../api/order'
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    data: {
      type: Array
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    dataList: [] as string[],
    isFresh: false as boolean,
    todayList: [] as string[],
    comID: -1 as number,
    show: false,
    isPicker: false,
    columns: [],
    repairPresonId: 0,
    repairPersonName: "",
    revokeReason: "",
    repairPresonFK: 0,
    requestID: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getList() {
      const comID = wx.getStorageSync("userInfo").data.comID;
      const userID = wx.getStorageSync("userInfo").data.userID
      loadingOrder(comID, userID).then((res: any) => {
        console.log('进行中今日', res)
        this.setData({
          dataList: res.data.data,
          isFresh: false
        })
      })
      todayOrder(comID, userID).then((res: any) => {
        console.log('进行中其他', res)
        this.setData({
          todayList: res.data.data,
          isFresh: false
        })
      })
    },
    fresh() {
      this.setData({
        isFresh: true,
      })
      this.triggerEvent('getSum')
      this.getList()
    },
    onTransfer(e: any) {
      this.setData({
        show: true,
        repairPresonFK: e.currentTarget.dataset.id,
        requestID: e.currentTarget.dataset.reqid,
      });
    },
    handleInput() {
      const that = this;
      const comID = wx.getStorageSync("userInfo").data.comID;
      const userID = wx.getStorageSync("userInfo").data.userID;
      searchMaintainer(comID, userID).then(function (e: any) {
        that.setData({ columns: e.data });
      }),
        this.setData({ isPicker: true });
    },
    onPickerClose() {
      this.setData({ isPicker: false });
    },
    onPicker(e: any) {
      this.setData({
        repairPresonId: e.detail.value.id,
        repairPersonName: e.detail.value.repairReason,
        isPicker: false,
      });
    },
    handleSubmit() {
      const that = this;
      const comID = wx.getStorageSync("userInfo").data.comID;
      const userID = wx.getStorageSync("userInfo").data.userID;
      const repairPresonId = that.data.repairPresonId;
      const repairPersonName = that.data.repairPersonName;
      const requestID = that.data.requestID;
      const repairPresonFK = that.data.repairPresonFK;
      const revokeReason = that.data.revokeReason;
      if (repairPresonId != 0 && revokeReason != "") {
        orderTransfer(comID, userID, repairPresonId, repairPersonName, requestID, repairPresonFK, revokeReason).then(function (e: any) {
          if (e.status === 200) {
            wx.showToast({ title: "转派成功", icon: "success" });
            that.setData({
              revokeReason: "",
              repairPresonId: 0
            });
            that.getList();
          } else {
            wx.showToast({ title: "转派失败", icon: "error" })
          }
        })
      } else {
        wx.showToast({ title: "缺少必填", icon: "error" });
      }

    },
  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      this.setData({
        comID: wx.getStorageSync("userInfo").data.comID,
        userID: wx.getStorageSync("userInfo").data.userID
      });
      this.getList();
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  pageLifetimes: {
    show: function () {
      // 页面被展示
      this.setData({
        comID: wx.getStorageSync("userInfo").data.comID,
        userID: wx.getStorageSync("userInfo").data.userID
      });
      this.getList();
    },
    hide: function () {
      // 页面被隐藏
    },
    resize: function () {
      // 页面尺寸变化
    }
  }
})